<template>
  <el-card>
    <template #header>
      {{ exData.id ? "编辑无障碍改造任务" : "新增无障碍改造任务" }}
    </template>
    <el-form
      ref="newTaskForm"
      class="newTask"
      :model="newTaskForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="newTaskForm.name"
          placeholder="请输入任务名称"
          clearable
        ></el-input>
      </el-form-item>
      <div
        v-for="(item, index) in newTaskForm.townList"
        :key="index"
        class="town"
      >
        <el-row :gutter="15">
          <el-col :span="17">
            <el-form-item
              :label="index===0?'选择乡镇':''"
              :prop="'townList.' + index + '.orgId'"
              :rules="townListRules.orgId"
            >
              <el-select v-model="item.orgId" placeholder="请选择乡镇">
                <el-option
                  v-for="item in options"
                  :key="item.origId"
                  :label="item.name"
                  :value="item.origId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              label-width="0"
              :prop="'townList.' + index + '.taskCount'"
              :rules="townListRules.taskCount"
            >
              <el-input-number
                v-model="item.taskCount"
                :min="1"
                :max="10"
                label="数量"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-tooltip content="新增乡镇" placement="top" v-if="index===0">
          <el-button
            type="success"
            icon="el-icon-plus"
            circle
            class="town__add"
            size="small"
            @click="addOrDelTownship()"
          />
        </el-tooltip>
        <el-tooltip content="删除乡镇" placement="top" v-else>
          <el-button
            type="danger"
            icon="el-icon-minus"
            circle
            class="town__del"
            size="small"
            @click="addOrDelTownship(index)"
          />
        </el-tooltip>
      </div>

      <el-form-item label="计划时间" prop="planTime">
        <el-date-picker
          v-model="planTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="改造牵头人" prop="lerderUserName">
        <el-input
          v-model="newTaskForm.lerderUserName"
          placeholder="请输入改造牵头人"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="lerderUserMobile">
        <el-input
          v-model="newTaskForm.lerderUserMobile"
          placeholder="请输入联系电话"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="exData.id" type="warning" @click="taskOut">下架</el-button>
        <el-button v-else type="primary" @click="taskSave">发布</el-button>
        <!-- <el-button type="success" @click="taskSave">发布</el-button> -->
        <el-button @click="clear()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
export default {
  name: "manageAdd",
  props: {
    obj: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    let validatePhone = (rule, value, callback) => {
      let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (reg.test(value)) {
        callback();
      } else {
        callback(new Error("手机号格式不正确"));
      }
    };
    return {
      townListRules: {
        orgId: [
          { required: true, message: "请选择乡镇", trigger: "blur" }
        ],
        taskCount: [
          { required: true, message: "请选择数量", trigger: "blur" },
        ]
      },
      exData: this.obj,
      newTaskForm: {
        townList: [
          { orgId: null, taskCount: 1 }
        ]
      },
      planTime: [],
      options: [],
      rules: {
        lerderUserMobile: [
          {
            required: false,
            message: "请输入联系电话",
            trigger: "blur"
          },
          { validator: validatePhone, trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    obj: {
      handler(val) {
        if (val.id) {
          this.API.getRemouldById({ id: val.id }).then(res => {
            this.newTaskForm = res.data;
            let { startTime, endTime } = res.data;
            this.planTime = [startTime, endTime];
          });
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    // 查询乡镇信息
    let query = { parentId: 1, type: 0, queryType: "ALL", flag: true };
    this.API.getOrganizeLazy(query).then(ret => {
      this.options = ret.data;
    });
  },
  methods: {
    // 新增或删除乡镇
    addOrDelTownship(index) {
      if (index) this.newTaskForm.townList.splice(index, 1)
      else this.newTaskForm.townList.push({ orgId: null, taskCount: 1 })
    },
    // 新增保存
    taskSave() {
      this.$refs["newTaskForm"].validate(valid => {
        if (valid) {
          this.$confirm("确认发布无障碍改造任务?", "提示", {
            type: "success"
          }).then(() => {
            let body = this.newTaskForm;
            body.startTime = this.planTime[0];
            body.endTime = this.planTime[1];
            // body.status = "SAVE";
            this.API.addOrUpdateRemould({ body }).then(res => {
              this.success("发布成功!");
              this.$refs["newTaskForm"].resetFields();
              this.$parent.who = "manageTable";
            });
          });
        } else {
          return false;
        }
      });
    },
    clear() {
      this.$parent.who = 'manageTable'
      this.$refs["newTaskForm"].resetFields();
      this.newTaskForm = {
        townList: [{ orgId: null, taskCount: 1 }]
      }
    },
    // 下架
    taskOut() {
      this.$confirm("确认下架无障碍改造任务?", "提示", {
        type: "warning"
      }).then(() => {
        this.API.updateData({ remouldId: this.exData.id, status: "DOWN" }).then(
          res => {
            this.success("下架成功!");
            this.$parent.show = "manageTable";
            this.$parent.getPageData();
          }
        );
      });
    }
  }
};
</script>
<style scoped lang="scss">
  .el-form-item {
    width: 500px;
  }

  .el-select {
    width: 100%;
  }

  .town {
    width: 500px;
    display: flex;
    align-items: center;
    position: relative;

    &__add {
      position: absolute;
      right: -50px;
      top: 6px
    }

    &__del {
      position: absolute;
      right: -50px;
      top: 4px
    }

    ::v-deep {
      .el-form-item {
        width: 100% !important;
      }
    }
  }
</style>
